from django.db import models
from django.contrib.auth.models import User


class Account(User):
    GENDER = ((1, '男'), (2, '女'), (3, '*'))
    ROLE = ((1, '管理员'), (2, '保留1'), (3, '保留2'), (4, '保留3'), (5, '保留4'),
            (5, '会员'), (6, '群主'), (7, '渠道'), (8, '游客'))
    gender = models.PositiveSmallIntegerField(choices=GENDER, blank=True, verbose_name="性别")
    mobile = models.CharField(max_length=11, blank=True, unique=True, verbose_name="手机号")
    role = models.PositiveSmallIntegerField(choices=ROLE, verbose_name="角色")
    avatar = models.CharField(max_length=255, null=True, blank=False, verbose_name="用户头像")
    on_delete = models.BooleanField(verbose_name="是否删除")


class WeChatAccount(models.Model):
    user = models.OneToOneField(Account, on_delete=models.CASCADE)
    nickName = models.CharField(max_length=100, null=True, blank=False)
    openId = models.CharField(max_length=255, null=True, blank=False)
    unionId = models.CharField(max_length=100, null=True, blank=False)
    city = models.CharField(max_length=100, null=True, blank=False)
    province = models.CharField(max_length=100, null=True, blank=False)
    country = models.CharField(max_length=100, null=True, blank=False)
    session_key = models.CharField(max_length=255, null=True, blank=False)

    def __str__(self):
        if self.nickName:
            return self.nickName
        else:
            return self.user.username


class SubscribeManager(models.Manager):
    # 查看所有粉丝
    def get_follower(self, user_id):
        return self.filter(follow_id=user_id)

    # 查看所有关注
    def get_follow(self, user_id):
        return self.filter(followed_id=user_id)


class Subscribe(models.Model):
    follow_id = models.PositiveIntegerField(verbose_name="关注用户ID")
    followed_id = models.PositiveIntegerField(verbose_name="被关注用户ID")
    create_at = models.DateTimeField(verbose_name="创建时间")

    objects = SubscribeManager()
